var data2xml = require('data2xml');
var convert  = data2xml();
var person = require('../api/person.js');

var http = require('http'),
	url = require('url'),
	//person = require('./person'),
	note = require('../api/note');

var c = convert(
    'pfif:person',
    {
        _attr : { 'xmlns:pfif' : 'http://zesty.ca/pfif/1.4' },
        'pfif:person_record_id' : 'A simple element',
        'pfif:entry_date':'arr'
    }
);
//Person元数据
var person_entry = {
	'person_record_id' : 1,
	'entry_date':1,
	'author_name':1,
	'author_email':1,
	'author_phone':1,
	'source_name':1,
	'source_date':1,
	'source_url':1,
	'full_name':1,
	'given_name':1,
	'family_name':1,
	'alternate_names':1,
	'profile_urls':1,
	'sex':1,
	'date_of_birth':1,//精度可调
	'age':1,//age可以是个范围
	'home_street':1,
	'home_neighborhood':1,
	'home_city':1,
	'home_state':1,
	'home_postal_code':1,
	'home_country':1,
	'photo_url':1,
	'description':1
};

//console.log(c);

http.createServer(function (request, response) {
	var rq_url_json = url.parse(request.url,true),
		rq_params = rq_url_json["query"],
		rq_method = request.method,
		rpson = "";
	if(rq_url_json.pathname == '/share'){
		person.get({'full_name':'erain'},{cb : function(err,array){
			if(err){
				//console.log("find err!");
			}else{
				// console.log(res);
				// rpson = ("code:0,msg:'suc',data:"+JSON.stringify(res));
				// console.log("find succ!")

				//console.log(array);
				var xarr = [],carr=[];

				for(item in array){
					//console.log(array[item]);
					var o = array[item],c={};

					for(v in o){
						if(person_entry[v]){
							c[v] = o[v];
						}
					}
					carr.push(c);
				}

				xarr.push(convert('pfif:pfif',{
						_attr:{'xmlns:pfif':'http://zesty.ca/pfif/1.4'},
						'pfif:person':carr
					}
				));
			}
			response.writeHead(200, {'Content-Type': 'text/xml'});
			response.end(xarr.join(''));
		}})
	}
}).listen(9999);